package com.biejiaogroup.librarymanager.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class GetSQLSession {
    /**
     * @param
     * @return org.apache.ibatis.session.SqlSession
     * @Description: 获取SqlSession对象
     * @Author: JesseChan
     * @Date:2022/5/4
     */
    public static SqlSession createSQLSession() {
        SqlSessionFactory sqlSessionFactory = null;
        InputStream inputStream = null;
        SqlSession session = null;
        try {
            String resource = "mybatis-config.xml";
            inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            session = sqlSessionFactory.openSession();
            return session;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args)
    {
        System.out.println(createSQLSession());
    }



}
